package ai.chat2db.server.web.start.config.oauth;

import ai.chat2db.server.domain.api.enums.RoleCodeEnum;
import ai.chat2db.server.domain.api.enums.ValidStatusEnum;
import ai.chat2db.server.domain.api.model.User;
import ai.chat2db.server.tools.base.excption.BusinessException;
import ai.chat2db.server.tools.common.model.LoginUser;
import ai.chat2db.server.web.start.controller.oauth.request.LoginRequest;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaTokenConsts;

import java.util.List;
import java.util.Objects;

/**
 * @author Asu
 * @version 1.0
 * @date 2024/5/20 10:11
 * @desc 用户验权service
 */
public interface OauthService {

    String oauthType();

    User login(LoginRequest loginRequest);

    LoginUser getLoginUser(String userId);

    List<String> getPermissionList();

    List<String> getRoleList();

    default void validateUser(final User user) {
        if (Objects.isNull(user)) {
            throw new BusinessException("oauth.userNameNotExits");
        }
        if (!ValidStatusEnum.VALID.getCode().equals(user.getStatus())) {
            throw new BusinessException("oauth.invalidUserName");
        }
        if (RoleCodeEnum.DESKTOP.getDefaultUserId().equals(user.getId())) {
            throw new BusinessException("oauth.IllegalUserName");
        }
    }

    default Object  doLogin(LoginRequest loginRequest) {
        User user = login(loginRequest);
        StpUtil.login(user.getId());
        return SaHolder.getStorage().get(SaTokenConsts.JUST_CREATED_NOT_PREFIX);
    }

}
